package de.clapp.toolkit;

import java.text.DecimalFormat;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import de.clapp.CLAppModel;
import de.clapp.backend.network.ClientRequest;
import de.clapp.backend.network.TCPSocket;
import de.clapp.backend.requests.ServerRequest;
import de.clapp.data.settings.ServerSettings;

public class Tools
{
    public static String CURRENT_CLUB = "CURRENTCLUB";
    public static DecimalFormat DEC_FORMAT = new DecimalFormat("#0.00");
    
    /**
     * Ersetzten eines vorhanden Layouts durch ein neues Fragment
     * 
     * @param targetFragment
     * @param targetLayout
     * @param fragment
     */
    public static void replaceFragment(final Fragment targetFragment, final int targetLayout, Fragment fragment)
    {
         FragmentTransaction transaction = targetFragment.getFragmentManager().beginTransaction();
         transaction.replace(targetLayout, fragment);
         transaction.addToBackStack(null);
         transaction.commit();
    }

    public static void takeServerRequest(final ServerRequest object)
    {
        Thread t = new Thread()
        {
            @Override
            public void run()
            {
                try
                {
                    ClientRequest r = new ClientRequest(new TCPSocket(de.clapp.Settings.ServerIP, ServerSettings.ServerPort));
                    r.request(object);
                    r.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        try
        {
            t.join(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    public static void takeClubRequest(final ServerRequest object)
    {
        Handler h = new Handler(){
            @Override
            public void handleMessage(Message msg)
            {
                Thread t = new Thread()
                {
                    @Override
                    public void run()
                    {
                        ClientRequest request = null;
                        try
                        {
                            /*
                             * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                             * Wegen der IP-Adresse muss das zum testen gemacht werden 
                             */
                            request = new ClientRequest(new TCPSocket(CLAppModel.getInstance().getCurrentClub().getIpAddress(), CLAppModel.getInstance().getCurrentClub().getPort()));
                            request.request(object);
                            request.close();
                            
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                };
                t.start();
                try
                {
                    t.join(1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            
        };
        h.handleMessage(null);
    }
}